const vm = require('vm');

const ctx = { priceStack: [1, 2, 3, 4, 5], triggerStack: [] };

let rtn = 100;
console.log('INIT rtn  %f , triggerStack %o ', rtn, ctx.triggerStack);

const sc = `
    (()=>{
        triggerStack.push(Math.random());
       return Math.random();
    })()
`

// console.log(ctx.triggerStack);

const script = new vm.Script(sc);
const vm_ctx = vm.createContext(Object.assign({}, ctx));

for (let i = 0; i < 5; i++) {
    rtn = script.runInContext(vm_ctx);
    console.log('rtn  %f , triggerStack %o ', rtn, ctx.triggerStack);
}

// Error test.
const sc2 = `lm;wlem;`
const script2 = new vm.Script(sc2);
const vm_ctx2 = vm.createContext(Object.assign({}, ctx));

try {
    rtn = script2.runInContext(vm_ctx2);
} catch (err) {
    console.error('Catch Error In Script');
}
